package com.qsmx.store.app.main.mem;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatSpinner;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.alibaba.fastjson.JSONObject;
import com.qsmx.store.app.R;
import com.qsmx.store.app.R2;
import com.qsmx.store.app.fustion.FustionManager;
import com.qsmx.store.app.fustion.FustionTag;
import com.qsmx.store.app.http.HttpHelper;
import com.qsmx.store.app.http.HttpUrl;
import com.qsmx.store.app.main.mem.entity.MemManagerEntity;
import com.qsmx.store.app.util.StringUtil;
import com.qsmx.store.core.delegates.AtmosDelegate;
import com.qsmx.store.core.net.callback.IError;
import com.qsmx.store.core.net.callback.IFailure;
import com.qsmx.store.core.net.callback.ISuccess;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MemCardPropertyGetCoinDelegate extends AtmosDelegate {

    private List<String> mCoinOutTypeList = new ArrayList<>();
    private ArrayAdapter typeArrayAdapter;
    private String mSelectType;
    private MemManagerEntity mMemManagerDate;

    @BindView(R2.id.tv_mem_card_use_time)
    AppCompatTextView tvMemCardUseTime;
    @BindView(R2.id.tv_mem_card_coins)
    AppCompatTextView tvMemCardCoins;
    @BindView(R2.id.et_mem_coin_out_num)
    AppCompatEditText etMemCoinOutNum;
    @BindView(R2.id.tv_mem_coin_last)
    AppCompatTextView tvMemCoinLast;
    @BindView(R2.id.sp_coin_out_type)
    AppCompatSpinner spCoinOutType;

    @OnClick(R2.id.tv_sure)
    void onSure() {
        JSONObject params = new JSONObject();
        params.put("deptId", FustionManager.mMachineData.getDeptId());
        params.put("memberNo", mMemManagerDate.getMemberNo());
        params.put("nums", etMemCoinOutNum.getText().toString());
        params.put("machineId", FustionManager.mMachineData.getMachineId());
        params.put("machineCode", FustionManager.mMachineData.getMachineCode());

        HttpHelper.RestClientPostRaw(params, HttpUrl.GET_COINS,
                new ISuccess() {
                    @Override
                    public void onSuccess(int code, String response) {

                    }
                }, new IError() {
                    @Override
                    public void onError(int code, String msg) {

                    }
                }, new IFailure() {
                    @Override
                    public void onFailure() {

                    }
                });

    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_mem_card_property_get_coin;
    }

    public static MemCardPropertyGetCoinDelegate create(MemManagerEntity memManagerData) {

        final Bundle args = new Bundle();
        args.putSerializable(FustionTag.MEM_CARD_INFO, memManagerData);

        final MemCardPropertyGetCoinDelegate delegate = new MemCardPropertyGetCoinDelegate();
        delegate.setArguments(args);
        return delegate;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Bundle args = getArguments();
        if (args != null) {
            mMemManagerDate = (MemManagerEntity) args.getSerializable(FustionTag.MEM_CARD_INFO);
        }
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {

        mCoinOutTypeList.add("人工出币");
        mCoinOutTypeList.add("出币机出币");

        tvMemCardUseTime.setText(StringUtil.getDateByTimestamp(StringUtil.getStringToDateTimeNoT(mMemManagerDate.getCreateTime()) + (long) (mMemManagerDate.getIcCard().getMemberGrade().getGradeValidDays() * 24 * 60 * 60 * 1000)));
        tvMemCardCoins.setText(String.valueOf(mMemManagerDate.getMemberProperty().getCoins()));
        tvMemCoinLast.setText(String.valueOf(mMemManagerDate.getMemberProperty().getCoins() - StringUtil.IntegerValueOf(etMemCoinOutNum.getText().toString(), 0)));

        typeArrayAdapter = new ArrayAdapter(getContext(), R.layout.item_mem_card_spinner_select, mCoinOutTypeList);
        typeArrayAdapter.setDropDownViewResource(R.layout.item_coin_out_type_drop);
        spCoinOutType.setAdapter(typeArrayAdapter);

        spCoinOutType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                mSelectType = mCoinOutTypeList.get(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }
}
